import java.io.*;

import javax.servlet.*;
import javax.servlet.http.*;

public class FourisseurAnnonces extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		
		System.out.println("servlet called");	
			
	    String line = null;  
	    StringBuilder builder = new StringBuilder();
	    
		// read body as String and response it to the client  
		try{
		BufferedReader br = request.getReader();  
		
		   
		      
		    while (br!=null && (line = br.readLine()) != null) {  
		    	builder.append(line);  
		    }  
		    br.close();  
		      
		   
		}catch(Exception e)
		{
			System.out.println(e.toString());
		}
		    
		    
		    
//		FileOutputStream fout;
//		
//		try {
//			// Open an output stream
//			fout = new FileOutputStream("AnnoncesRecus.txt");
//
//			// Print a line of text
//			new PrintStream(fout).println("got a request: "+responseResult.toString());
//
//			// Close our output stream
//			fout.close();
//		}
//		// Catches any error conditions
//		catch (IOException e) {
//			System.err.println("Unable to write to file");
//			System.exit(-1);
//		}
		
		
		 BufferedWriter bw = null;

	      try {
	     bw = new BufferedWriter(new FileWriter("C:/Program Files/Apache Software Foundation/Tomcat 7.0/webapps/examples/WEB-INF/classes/AnnoncesRecus.txt", true));
		 bw.write("got a request: " + builder.toString());
		 bw.newLine();
		 bw.flush();
	      } catch (IOException ioe) {
		 ioe.printStackTrace();
	      } finally {                       // always close the file
		 if (bw != null) try {
		    bw.close();
		 } catch (IOException ioe2) {
		    // just ignore it
		 }
	      } // end try/catch/finally
		

		System.out.println();
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out.println("<html>");
		out.println("<head>");
		out.println("<title>Hello!</title>");
		out.println("</head>");
		out.println("<body>");
		out.println("<h1>Hello!</h1>");
		out.println("</body>");
		out.println("</html>");

	}
	
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		
		System.out.println("servletpost called");
		 //ServletInputStream istr = request.getInputStream();  
	  //System.out.println(istr);
		
	
	    String line = null;  
	    StringBuilder builder = new StringBuilder();
	    
		// read body as String and response it to the client  
		try{
		BufferedReader br = request.getReader();  
		
		   
		      
		    while (br!=null && (line = br.readLine()) != null) {  
		    	builder.append(line);  
		    }  
		    br.close();  
		      
		   
		}catch(Exception e)
		{
			System.out.println(e.toString());
		}
		    
		    
		    
//		FileOutputStream fout;
//		
//		try {
//			// Open an output stream
//			fout = new FileOutputStream("AnnoncesRecus.txt");
//
//			// Print a line of text
//			new PrintStream(fout).println("got a request: "+responseResult.toString());
//
//			// Close our output stream
//			fout.close();
//		}
//		// Catches any error conditions
//		catch (IOException e) {
//			System.err.println("Unable to write to file");
//			System.exit(-1);
//		}
		
		
		 BufferedWriter bw = null;

	      try {
	     bw = new BufferedWriter(new FileWriter("C:/Program Files/Apache Software Foundation/Tomcat 7.0/webapps/examples/WEB-INF/classes/AnnoncesRecus.txt", true));
		 bw.write("got a request: " + builder.toString());
		 bw.newLine();
		 bw.flush();
	      } catch (IOException ioe) {
		 ioe.printStackTrace();
	      } finally {                       // always close the file
		 if (bw != null) try {
		    bw.close();
		 } catch (IOException ioe2) {
		    // just ignore it
		 }
	      } // end try/catch/finally
		

		System.out.println();
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out.println("<html>");
		out.println("<head>");
		out.println("<title>Hello!</title>");
		out.println("</head>");
		out.println("<body>");
		out.println("<h1>Hello!</h1>");
		out.println("</body>");
		out.println("</html>");

	}
	

	
	
}
